package com.keon.jianbao.api;

import android.content.Context;

import com.keon.jianbao.AppContext;
import com.keon.jianbao.util.TLog;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import org.apache.http.entity.StringEntity;

import java.io.UnsupportedEncodingException;

/**
 * Created by keon on 2015/7/17.
 */
public class ApiHttpClient {
    public final static String HOST = "http://freshlocpack1.gotoip1.com/";
    private static String API_URL = "http://freshlocpack1.gotoip1.com/%s";
    public static AsyncHttpClient client;

    private static String appCookie;

    public static AsyncHttpClient getHttpClient() {
        return client;
    }

    public static void setHttpClient(AsyncHttpClient c) {
        client = c;
//        client.addHeader("Accept-Language", Locale.getDefault().toString());
//        client.addHeader("Host", HOST);
//        client.addHeader("Connection", "Keep-Alive");
//        client.getHttpClient().getParams()
//                .setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);
    }

    public static void cancelAll(Context context) {
        client.cancelRequests(context, true);
    }

    public static void g(String url, AsyncHttpResponseHandler handler) {
        client.get(url, handler);
        log(new StringBuilder("GET ").append(url).toString());
    }

    public static void post(String partUrl, AsyncHttpResponseHandler handler) {
        client.post(getAbsoluteApiUrl(partUrl), handler);
        log(new StringBuilder("POST ").append(partUrl).toString());
    }

    public static void post(String partUrl, RequestParams params,
                            AsyncHttpResponseHandler handler) {

//        client.addHeader("COOKIES", "PHPSESSID=" + AppContext.getInstance().getProperty("user.sessionid"));

        client.post(getAbsoluteApiUrl(partUrl), params, handler);
        log(new StringBuilder("POST ").append(partUrl).append("&")
                .append(params).toString());
    }

    public static void get(String partUrl, AsyncHttpResponseHandler handler) {
        client.get(getAbsoluteApiUrl(partUrl), handler);
        log(new StringBuilder("GET ").append(partUrl).toString());
    }

    public static void get(String partUrl, RequestParams params,
                           AsyncHttpResponseHandler handler) {
        client.get(getAbsoluteApiUrl(partUrl), params, handler);
        log(new StringBuilder("GET ").append(partUrl).append("&")
                .append(params).toString());
    }

    public static void put(String partUrl, AsyncHttpResponseHandler handler) {
        client.put(getAbsoluteApiUrl(partUrl), handler);
        log(new StringBuilder("PUT ").append(partUrl).toString());
    }

    public static void put(String partUrl, RequestParams params,
                           AsyncHttpResponseHandler handler) {
        client.put(getAbsoluteApiUrl(partUrl), params, handler);
        log(new StringBuilder("PUT ").append(partUrl).append("&")
                .append(params).toString());
    }

    public static String getAbsoluteApiUrl(String partUrl) {
        String url = String.format(API_URL, partUrl);
        log("request:" + url);
        return url;
    }

    public static void log(String log) {
        TLog.log("http", log);
    }

    public static void setCookie(String cookie) {
        client.addHeader("Cookie", cookie);
    }

    public static String getCookie(AppContext appContext) {
        if (appCookie == null || appCookie == "") {
            appCookie = appContext.getProperty("cookie");
        }
        return appCookie;
    }

    public static void cleanCookie() {
        appCookie = "";
    }

    public static void postJson(String url, String json, AsyncHttpResponseHandler handler) throws UnsupportedEncodingException {
        StringEntity entity = new StringEntity(json);
        client.post(AppContext.getContext(), url, entity, "application/json; charset=utf-8", handler);
    }
}